<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="120px">
				<el-row :gutter="20">
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item ref="Entity_Name_FormItem" prop="Entity.Name" label="名称">
							<el-input v-model="stateData.vmModel.Entity.Name" disabled class="width100"></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item ref="Entity_ClassName_FormItem" prop="Entity.ClassName" label="类名">
							<el-input v-model="stateData.vmModel.Entity.ClassName" disabled class="width100"></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item ref="Entity_Description_FormItem" prop="Entity.Description" label="描述">
							<el-input v-model="stateData.vmModel.Entity.Description" disabled class="width100"></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item
							ref="Entity_TaskType_FormItem"
							prop="Entity.TaskType"
							label="任务类型"
							:rules="[{ required: true, message: '任务类型为必填项', trigger: 'blur' }]"
						>
							<el-select v-model="stateData.vmModel.Entity.TaskType" disabled class="width100">
								<el-option key="单次任务" value="单次任务" label="单次任务"></el-option>
								<el-option key="周期任务" value="周期任务" label="周期任务"></el-option>
								<el-option key="定时任务" value="定时任务" label="定时任务"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item ref="Entity_StartTime_FormItem" prop="Entity.StartTime" label="上次运行开始时间">
							<el-date-picker v-model="stateData.vmModel.Entity.StartTime" disabled class="width100"></el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item ref="Entity_EndTime_FormItem" prop="Entity.EndTime" label="上次运行结束时间">
							<el-date-picker v-model="stateData.vmModel.Entity.EndTime" disabled class="width100"></el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item ref="Entity_Repeat_FormItem" prop="Entity.Repeat" label="重复次数">
							<el-input-number v-model="stateData.vmModel.Entity.Repeat" disabled class="width100"></el-input-number>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item ref="Entity_IsForever_FormItem" prop="Entity.IsForever" label="是否永久重复">
							<el-select v-model="stateData.vmModel.Entity.IsForever" disabled class="width100">
								<el-option :key="1" :value="true" :label="$t('message._system.common.vm.tips_bool_true')"></el-option>
								<el-option :key="0" :value="false" :label="$t('message._system.common.vm.tips_bool_false')"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item ref="Entity_IntervalInSeconds_FormItem" prop="Entity.IntervalInSeconds" label="延迟秒数">
							<el-input-number v-model="stateData.vmModel.Entity.IntervalInSeconds" disabled class="width100"></el-input-number>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item ref="Entity_DelaySeconds_FormItem" prop="Entity.DelaySeconds" label="延迟秒数">
							<el-input-number v-model="stateData.vmModel.Entity.DelaySeconds" disabled class="width100"></el-input-number>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item ref="Entity_LastRunTime_FormItem" prop="Entity.LastRunTime" label="上次运行时长">
							<el-input-number v-model="stateData.vmModel.Entity.LastRunTime" disabled class="width100"></el-input-number>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item ref="Entity_Cron_FormItem" prop="Entity.Cron" label="Cron表达式">
							<el-input v-model="stateData.vmModel.Entity.Cron" disabled class="width100"></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item ref="Entity_LastRunResult_FormItem" prop="Entity.LastRunResult" label="上次运行状态">
							<el-select v-model="stateData.vmModel.Entity.LastRunResult" disabled class="width100">
								<el-option key="初始化" value="初始化" label="初始化"></el-option>
								<el-option key="失败" value="失败" label="失败"></el-option>
								<el-option key="成功" value="成功" label="成功"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item
							ref="Entity_IsEnable_FormItem"
							prop="Entity.IsEnable"
							label="是否启用"
							:rules="[{ required: true, message: '是否启用为必填项', trigger: 'blur' }]"
						>
							<el-select v-model="stateData.vmModel.Entity.IsEnable" disabled class="width100">
								<el-option :key="1" :value="true" :label="$t('message._system.common.vm.tips_bool_true')"></el-option>
								<el-option :key="0" :value="false" :label="$t('message._system.common.vm.tips_bool_false')"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item
							ref="Entity_IsRun_FormItem"
							prop="Entity.IsRun"
							label="是否运行"
							:rules="[{ required: true, message: '是否运行为必填项', trigger: 'blur' }]"
						>
							<el-select v-model="stateData.vmModel.Entity.IsRun" disabled class="width100">
								<el-option :key="1" :value="true" :label="$t('message._system.common.vm.tips_bool_true')"></el-option>
								<el-option :key="0" :value="false" :label="$t('message._system.common.vm.tips_bool_false')"></el-option>
							</el-select>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item ref="Entity_CreateTime_FormItem" prop="Entity.CreateTime" label="CreateTime">
							<el-date-picker v-model="stateData.vmModel.Entity.CreateTime" disabled class="width100"></el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :lg="12" class="mb20">
						<el-form-item ref="Entity_UpdateTime_FormItem" prop="Entity.UpdateTime" label="UpdateTime">
							<el-date-picker v-model="stateData.vmModel.Entity.UpdateTime" disabled class="width100"></el-date-picker>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
		</el-card>
	</div>
</template>

<!--suppress JSAnnotator -->
<script setup lang="ts" name="message._system.common.vm.edit;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted, computed, nextTick, ComponentInternalInstance } from 'vue';
import { SysTaskV2Api as api } from '/@/views/_admin/systaskv2/api';
import other from '/@/utils/other';
import { ISysTaskV2Vm } from '/@/views/_admin/systaskv2/api/interface';
import { ISysSelectType, IViewModel } from '/@/types/common';

const BUTTON_TEXTS = {
	SAVE: '保存',
	SUBMIT: '提交',
	AGREE: '同意',
	REJECT: '拒绝',
	CLOSE: '关闭',
} as const;
const ci = getCurrentInstance() as ComponentInternalInstance;
const loading = ref(false);
const formRef = ref();
const stateData = reactive<IViewModel<ISysTaskV2Vm>>({
	vmModel: {
		Entity: {
			ID: null,
			Name: null,
			ClassName: null,
			Description: null,
			TaskType: '单次任务',
			StartTime: null,
			EndTime: null,
			Repeat: null,
			IsForever: null,
			IntervalInSeconds: null,
			DelaySeconds: null,
			LastRunTime: null,
			Cron: null,
			LastRunResult: null,
			IsEnable: false,
			IsRun: false,
			CreateTime: new Date(),
			UpdateTime: new Date(),
		},

		ActionName: null,
		IsMyApprove: null,
	},
});

// 取消
const onClose = () => {
	closeDialog();
};

// 加载选择列表数据
const loadData = async () => {
	await Promise.allSettled([
		api()
			.get(stateData.vmModel.Entity.ID ?? '')
			.then((x) => other.setValue(stateData.vmModel, x)),
	]);
};

const initializePageData = () => {
	const wtmdata = ci.attrs['wtmdata'] as any;
	if (wtmdata?.ID) {
		stateData.vmModel.Entity.ID = wtmdata.ID;
	}
};
// 页面加载时
onMounted(async () => {
	loading.value = true;
	initializePageData();

	try {
		// 并行加载选择列表数据
		await loadData();
	} catch (error) {
		other.setFormError(ci, error);
	} finally {
		loading.value = false;
		nextTick(() => {
			setupHeaderButtons();
		});
	}
});

// 定义子组件向父组件传值事件
const emit = defineEmits(['refresh', 'closeDialog', 'updateHeaderButtons']);

// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};

// 设置对话框头部按钮
const setupHeaderButtons = () => {
	const headerButtons = [
		{
			text: BUTTON_TEXTS.CLOSE,
			type: 'default',
			onClick: onClose,
			visible: true,
		},
	];
	emit('updateHeaderButtons', headerButtons);
};
</script>
